<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Using JDO with Google AppEngine Datastore | Blogs at
	Codeplus Technologies</title>
<meta name="description"
	content="This post is a walk-through/how-to on using JDO with Google AppEngine Datastore. It also differentiates between Datastore API and JDO over Datastore usage" />
<%@ include file="/view/layout/commonref.jsp"%>
</head>
<body>
	<div id="root">
		<div id="banner">
			<%@ include file="/view/layout/banner.jsp"%>
		</div>
		<div id="left-panel">
			<%@ include file="/view/layout/leftpanel.jsp"%>
		</div>
		<div id="body-panel"
			style="height: 1500px; border: 1px solid #dedede; border-top: none; padding: 20px;"
			class="shadow">
			<h2>Using JDO with Google AppEngine Datastore</h2>
			<p>
				Today we’ll walk through on steps how to perform an insert into <a
					href="http://code.google.com/appengine/docs/java/gettingstarted/usingdatastore.html">Google
					AppEngine datastore</a> using <a
					href="http://code.google.com/appengine/docs/java/datastore/"
					target="_blank">Datastore API</a> and <a
					href="http://code.google.com/appengine/docs/java/datastore/jdo/"
					target="_blank">JDO</a>.
			</p>
			<p>
				In my last project I used <a
					href="http://code.google.com/appengine/docs/java/gettingstarted/usingdatastore.html">Google
					AppEngine datastore</a> without <a
					href="http://code.google.com/appengine/docs/java/datastore/jdo/">JDO
					or JPA</a>. It was fun. Well, I had to do a lot of transformations on
				my own from Entities to POJOs. Something like the following:
			</p>
			<blockquote>				
				<code>Entity brandEntity = new
					Entity(Brand.class.getSimpleName()); </code>
				<code>brandEntity.setProperty("name", brand.getName()); </code>
				<code>brandEntity.setProperty("description",
					brand.getDescription()); </code>				
			</blockquote>
			<p>With JDO you don’t have to worry about it. You need to worry
				about your Java objects and not Datastore entities. So, no more
				string literals and you are all set with type safety. The above code
				will look similar to something like the following:</p>
			<blockquote>
				<code>Brand brand = new Brand(); </code>
				<code>brand.setName("Xbox 360"); </code>
				<code>brand.setDescription("Gaming console by Microsoft");</code>
			</blockquote>
			<p>Using Datastore API you insert your data using the following:</p>
			<blockquote>
				<code>DatastoreService datastore = Datastore.get(); </code>
				<code>Entity brandEntity = new
					Entity(Brand.class.getSimpleName()); </code>
				<code>brandEntity.setProperty("name", brand.getName()); </code>
				<code>brandEntity.setProperty("description",
					brand.getDescription());</code>
				<code>datastore.put(brandEntity);</code>
			</blockquote>
			<p>Datastore.java is a custom class defined as:</p>
			<blockquote>
				<code>public class Datastore </code>
				<code>{</code>
				<code style="padding-left: 16px;"> private static final
					DatastoreService datastore = </code>
				<code style="padding-left: 32px;"> DatastoreServiceFactory
					.getDatastoreService();</code>
				<code style="padding-left: 16px;"> private Datastore() { }</code>
				<code style="padding-left: 16px;"> public static
					DatastoreService get() </code>
				<code style="padding-left: 16px;">{ </code>
				<code style="padding-left: 32px;">return datastore;//returns
					the singleton instance </code>
				<code style="padding-left: 16px;">} </code>
				<code>} </code>
			</blockquote>
			<p>Using JDO, the same can be achieved by the following:</p>
			<blockquote>
				<code>PersistenceManager db =
					Persistancemanager.getPersistenceManager(); </code>
				<code>Brand brand = new Brand(); </code>
				<code>brand.setName("Xbox 360"); </code>
				<code>brand.setDescription("Gaming console by Microsoft"); </code>
				<code>try </code>
				<code>{ </code>
				<code style="padding-left: 16px;">db.makePersistent(brand); </code>
				<code>}</code>
				<code> finally </code>
				<code>{</code>
				<code style="padding-left: 16px;"> db.close(); </code>
				<code>}</code>
			</blockquote>
			<p>Persistencemanager.java is a custom class that is a wrapper
				over JDO PersistenceManager defined as following:</p>
			<blockquote>
				<code>public final class Persistancemanager</code>
				<code> {</code>
				<code style="padding-left: 16px;"> private static final
					PersistenceManagerFactory pmfInstance = </code>
				<code style="padding-left: 32px;">JDOHelper
					.getPersistenceManagerFactory("transactions-optional"); </code>
				<code style="padding-left: 16px;">private
					Persistancemanager() { } </code>
				<code style="padding-left: 16px;">public static
					PersistenceManagerFactory getInstance() </code>
				<code style="padding-left: 16px;">{ </code>
				<code style="padding-left: 32px;">return pmfInstance; </code>
				<code style="padding-left: 16px;">} </code>
				<code style="padding-left: 16px;">public static
					PersistenceManager getPersistenceManager() </code>
				<code style="padding-left: 16px;">{ </code>
				<code style="padding-left: 32px;">return
					pmfInstance.getPersistenceManager(); </code>
				<code style="padding-left: 16px;">} </code>
				<code>}</code>
			</blockquote>
			<p>The Google AppEngine Datastore is schemaless. JDO and/or JPA
				provides a way to add a soft schema layer on top of the datastore.
				So, apart from the benefit of CRUD-ing data easily we can also
				enforce a lot of application level constraints like association
				between entities and so on.</p>
			<p>I am sure the above is going to help a lot of beginners and
				Google AppEngine enthusiasts in getting upto speed with the
				datastore.</p>
		</div>
	</div>
	<div id="right-panel">
		<%@ include file="/view/layout/rightpanel.jsp"%>
	</div>
	<div id="footer">
		<%@ include file="/view/layout/footer.jsp"%>
	</div>
</body>
</html>